const express = require('express');
const proxy = require("http-proxy-middleware");
const app = express();
const path = require("path")
// User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36
// gen/realizeActivityEvent/queryListEvent
app.use('/rrrrr/**',
    proxy((pathname, req) => {
        console.log(pathname)
        // console.log(req)
        return true;
    }, {
        // 代理目标地址
        target: "http://172.17.125.84:8030", // 后台接口域名 
        // ws: true,        //如果要代理 websockets，配置这个参数
        secure: false,  // 如果是https接口，需要配置这个参数
        changeOrigin: true,  //是否跨域 
        onProxyReq: function (proxyReq, req, res, options) {
            // console.log(res)
            // console.log(req.body)
            proxyReq.setHeader('cookie', 'webossid=webos00.17968C9B69D7E7FB4F7CA1034003DCB3; JSESSIONID=node01fpmsy41l6b45ljycagi465dt494.node0');
            // User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6.1 Safari/605.1.15
            proxyReq.setHeader('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6.1 Safari/605.1.15');
            proxyReq.setHeader('Referer', 'https://bboss.test.bestpay.net/PF/page/mallOrderService/queryPage.html');
            // Referer: https://bboss.test.bestpay.net/PF/page/mallOrderService/queryPage.html
            if (req.body) {
                let bodyData = JSON.stringify(req.body);
                // incase if content-type is application/x-www-form-urlencoded -> we need to change to application/json
                // proxyReq.setHeader('Content-Type', 'application/json');
                // proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));

                // stream the content
                console.log(bodyData)
                proxyReq.write(bodyData);
            }
        },
        pathRewrite: {
            // 地址重写
            // "/api": ""
        }
    })
    //     .on('proxyReq', function onProxyReq(proxyReq, req, res) {

    //   })
);
// app.use('/taiping-attendance-management/**',
//   proxy((pathname, req) => {
//     console.log(pathname)
//     // console.log(req)
//    return true;
//   },{
//     // 代理目标地址
//     target: 'http://127.0.0.1:8082',
//     changeOrigin: true,
//     // pathRewrite: {
//     // 地址重写
//     // "/ibot": "/ibot"
//     // }
//   }));
//   '/taiping-attendance-management': {
//      target: 'http://127.0.0.1:8082',
//     //target:'http://10.28.151.13:8082',
//     changeOrigin: true,
//     pathRewrite: {
//       // '^/taiping-attendance-management': ''
//     }
//   }

// app.use("/",
//   proxy({
//     // 这里是vue/react启动后需要访问网页的地址
//     // **修改了这里
//     target: "http://localhost:8080",
//     // target: "http://zyadmin.xiaoluge.top",
//     // target: "http://106.12.76.26:7001",
//     changeOrigin: true,
//     ws: true,        //如果要代理 websockets，配置这个参数
//     secure: false,  // 如果是https接口，需要配置这个参数
//     changeOrigin: true,  //是否跨域 
//   }))


app.use(express.static(path.join(__dirname, 'assets')))
app.listen(8288, () => {
    console.log("项目启动与：http://localhost:8288");
})